home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pnl010.zip / BIGARRAY.PAS < prev    next >
Pascal/Delphi Source File  |  1992-03-01  |  3KB  |  99 lines

  1. unit BigArray;
  2.  
  3. {Program to accompany article in issue #10 of the Pascal NewsLetter.     }
  4. {Author: Mitch Davis, (3:634/384.6) +61-3-890-2062.                      }
  5.  
  6. { from public domain ideas by Trevor J. Carlsen, 3:690/644 }
  7.  
  8. {This unit lets you implement whopping great big arrays, larger than the }
  9. {64k offered by Turbo Pascal.  It does this by offering a large array    }
  10. {object.                                                                 }
  11. {The array can have elements of any size, and the array can take all free}
  12. {DOS memory.  Thus, you could conceiveably have an array of reals taking }
  13. {some 400k or more.  Each array can take a subscript in the range of 1 to}
  14. {the parameter you hand the Init method - ie, the array is one-based.    }
  15.  
  16. {Note this code has NOT been optimised - it needs it very badly!         }
  17. {A future version of this unit (to be published in the PNL) will be      }
  18. {heavily optimised, will offer multidimensional arrays, and will offer   }
  19. {arrays that spill over onto disk, as well as specific support for sparse}
  20. {arrays.  Keep your eyes out for it!                                     }
  21.  
  22. {This program has NOT been proven correct.  I know it works when used as }
  23. {part of the MDP programs published in PNL #10, but I can't vouch for its}
  24. {operation if and when used in different situations.                     }
  25.  
  26. {I suggest compiling this in 286 mode, at least until I can get around to}
  27. {optimising it!                                                          }
  28.  
  29. {$R-,S-,N+}
  30.  
  31. {$DEFINE OnA286} {Remove this line if you're not on a 186 or higher.}
  32.  
  33. {$IFDEF OnA286} {$G+} {$ELSE} {$G-} {$ENDIF}
  34.  
  35. interface
  36.  
  37. {$IFDEF OnA286}
  38. uses Test186;
  39. {$ENDIF}
  40.  
  41. type BigDOSArray = object
  42.                      procedure SetElemSize (newsize:word);
  43.                      function GetMaxSize:longint;
  44.                      procedure Init (Elems:longint);
  45.                      function Elem (elemnum:longint):pointer;
  46.                      procedure Done;
  47.                    private
  48.                      base:word; {paragraph of base}
  49.                      linear:longint;
  50.                      size:word; {size of each element}
  51.                    end;
  52.  
  53. implementation
  54.  
  55. uses DosMem;
  56.  
  57. procedure BigDOSArray.SetElemSize;
  58.  
  59. begin
  60.   size := newsize;
  61. end;
  62.  
  63. function BigDOSArray.GetMaxSize;
  64.  
  65. var paras:word;
  66.     bytes:longint;
  67.     elemss:longint;
  68.  
  69. begin
  70.   GetMaxSize := pred ((longint (DosMem.Largest) shl 4) div size);
  71. end;
  72.  
  73. procedure BigDOSArray.Init;
  74.  
  75. var bytes:longint;
  76.     paras:word;
  77.  
  78. begin
  79.   base := DosMem.Alloc (succ((longint(Elems) * size) shr 4));
  80.   linear := longint (base) shl 4;
  81. end;
  82.  
  83. function BigDOSArray.Elem;
  84.  
  85. var offset:longint;
  86.  
  87. begin
  88.   offset := pred(elemnum) * size + linear;
  89.   Elem := ptr (offset shr 4,offset and $f);
  90. end;
  91.  
  92. procedure BigDOSArray.Done;
  93.  
  94. begin
  95.   DosMem.Free (base);
  96. end;
  97.  
  98. end.
  99.